#include"Stack.h"
#include"MyQueue.h"

MyQueue* myQueueCreate() 
{
    MyQueue* ret = malloc(sizeof(MyQueue));
    ret->inStack = stackCreate(100);
    ret->outStack = stackCreate(100);
    return ret;
}

void in2out(MyQueue* obj) 
{
    while (!stackEmpty(obj->inStack)) 
    {
        stackPush(obj->outStack, stackTop(obj->inStack));
        stackPop(obj->inStack);
    }
}

void myQueuePush(MyQueue* obj, int x) 
{
    stackPush(obj->inStack, x);
}

int myQueuePop(MyQueue* obj) 
{
    if (stackEmpty(obj->outStack)) 
    {
        in2out(obj);
    }
    int x = stackTop(obj->outStack);
    stackPop(obj->outStack);
    return x;
}

int myQueuePeek(MyQueue* obj) 
{
    if (stackEmpty(obj->outStack)) 
    {
        in2out(obj);
    }
    return stackTop(obj->outStack);
}

bool myQueueEmpty(MyQueue* obj) 
{
    return stackEmpty(obj->inStack) && stackEmpty(obj->outStack);
}

void myQueueFree(MyQueue* obj) 
{
    stackFree(obj->inStack);
    stackFree(obj->outStack);
}